<template>
  <view class="container">
    <!-- 数据录入表单 -->
    <view class="input-card">
      <view class="section-title">今日身体数据</view>
      
      <view class="input-group">
        <text class="label">体重 (kg)</text>
        <input v-model="todayData.weight" type="number" placeholder="50.0" />
      </view>

      <view class="input-group">
        <text class="label">BMI</text>
        <input v-model="todayData.bmi" type="number" placeholder="自动计算" disabled />
      </view>

      <view class="input-row">
        <view class="input-group">
          <text class="label">收缩压</text>
          <input v-model="todayData.systolic" type="number" placeholder="120" />
        </view>
        <view class="input-group">
          <text class="label">舒张压</text>
          <input v-model="todayData.diastolic" type="number" placeholder="80" />
        </view>
      </view>

      <view class="input-row">
        <view class="input-group">
          <text class="label">心率</text>
          <input v-model="todayData.heartRate" type="number" placeholder="75" />
        </view>
        <view class="input-group">
          <text class="label">血糖(mmol/L)</text>
          <input v-model="todayData.bloodSugar" type="number" placeholder="5.0" />
        </view>
      </view>

      <button class="save-btn" @click="saveData">保存今日数据</button>
    </view>

    <!-- 健康建议 -->
    <view class="advice-card" v-if="healthAdvice">
      <view class="section-title">健康建议</view>
      <view class="advice-item" v-for="(item, index) in healthAdvice" :key="index">
        <text class="advice-icon">💡</text>
        {{ item }}
      </view>
    </view>

    <!-- 历史趋势 -->
    <view class="history-card">
      <view class="section-title">历史趋势</view>
      
      <view class="history-list">
        <view class="history-item" v-for="(item, index) in historyData" :key="index">
          <text class="date">{{ formatDate(item.date) }}</text>
          <text class="weight">{{ item.weight }}kg</text>
          <text class="delete" @click="deleteRecord(index)">×</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

// 当日数据
const todayData = ref({
  weight: '',
  bmi: '',
  systolic: '',
  diastolic: '',
  heartRate: '',
  bloodSugar: '',
  date: new Date()
})

// 历史数据
const historyData = ref([])

// 初始化加载数据
onMounted(() => {
  const saved = uni.getStorageSync('healthData')
  if (saved) historyData.value = JSON.parse(saved)
})

// 自动计算BMI
const calculateBMI = (weight, height = 1.75) => { // 假设固定身高，实际应允许用户设置
  if (!weight) return ''
  return (weight / (height * height)).toFixed(1)
}

// 健康建议生成
const healthAdvice = computed(() => {
  const advice = []
  const d = todayData.value

  // 体重建议
  if (d.weight) {
    const bmi = calculateBMI(d.weight)
    if (bmi < 18.5) {
      advice.push('体重过轻，建议增加营养摄入')
    } else if (bmi > 24) {
      advice.push('体重超标，建议适当运动控制饮食')
    }
  }

  // 血压建议
  if (d.systolic && d.diastolic) {
    if (d.systolic > 140 || d.diastolic > 90) {
      advice.push('血压偏高，建议就医检查')
    } else if (d.systolic < 90 || d.diastolic < 60) {
      advice.push('血压偏低，注意补充营养')
    }
  }

  // 心率建议
  if (d.heartRate) {
    if (d.heartRate > 100) {
      advice.push('心率偏快，建议避免剧烈运动')
    } else if (d.heartRate < 60) {
      advice.push('心率偏慢，如有不适请及时就医')
    }
  }

  // 血糖建议
  if (d.bloodSugar) {
    if (d.bloodSugar > 7.0) {
      advice.push('空腹血糖偏高，建议控制碳水摄入')
    } else if (d.bloodSugar < 3.9) {
      advice.push('血糖偏低，注意及时补充能量')
    }
  }

  return advice
})

// 保存数据
const saveData = () => {
  if (!todayData.value.weight) {
    uni.showToast({ title: '请至少填写体重', icon: 'none' })
    return
  }

  const record = {
    ...todayData.value,
    date: new Date().toISOString(),
    bmi: calculateBMI(todayData.value.weight)
  }

  historyData.value.unshift(record)
  uni.setStorageSync('healthData', JSON.stringify(historyData.value))
  uni.showToast({ title: '保存成功' })
  resetForm()
}

// 删除记录
const deleteRecord = (index) => {
  uni.showModal({
    title: '确认删除',
    content: '确定要删除这条记录吗？',
    success: (res) => {
      if (res.confirm) {
        historyData.value.splice(index, 1)
        uni.setStorageSync('healthData', JSON.stringify(historyData.value))
      }
    }
  })
}

// 重置表单
const resetForm = () => {
  todayData.value = {
    weight: '',
    bmi: '',
    systolic: '',
    diastolic: '',
    heartRate: '',
    bloodSugar: '',
    date: new Date()
  }
}

// 日期格式化
const formatDate = (dateStr) => {
  const date = new Date(dateStr)
  return `${date.getMonth()+1}-${date.getDate()}`
}
</script>

<style scoped>
.container {
  padding: 20rpx;
  background-color: #f5f5f5;
}

.section-title {
  font-size: 32rpx;
  color: #333;
  font-weight: bold;
  margin-bottom: 30rpx;
}

.input-card, .advice-card, .history-card {
  background: white;
  border-radius: 16rpx;
  padding: 30rpx;
  margin-bottom: 30rpx;
}

.input-group {
  margin-bottom: 30rpx;
}

.label {
  display: block;
  color: #666;
  font-size: 28rpx;
  margin-bottom: 15rpx;
}

input {
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  font-size: 28rpx;
}

.input-row {
  display: flex;
  gap: 30rpx;
}
.input-row > view {
  flex: 1;
}

.save-btn {
  background: #00C853;
  color: white;
  height: 88rpx;
  line-height: 88rpx;
  border-radius: 44rpx;
  margin-top: 40rpx;
}

.advice-item {
  padding: 20rpx;
  background: #FFF9C4;
  border-radius: 8rpx;
  margin-bottom: 20rpx;
  font-size: 28rpx;
  color: #666;
}

.advice-icon {
  margin-right: 15rpx;
}

.mock-chart {
  height: 400rpx;
  background: #f8f8f8;
  border-radius: 16rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #999;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx 0;
  border-bottom: 1rpx solid #eee;
}

.date {
  color: #999;
  font-size: 26rpx;
}

.weight {
  color: #333;
  font-weight: bold;
}

.delete {
  color: #ff4444;
  font-size: 40rpx;
  padding: 0 20rpx;
}
</style>